// pages/productDetails/productDetails.js
//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    /*banenr */
    banner: [
      '../../images/proddetadil_1.jpg',
      '../../images/proddetadil_1.jpg',
    ],
    indicatorDots: false,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    windowWidth: 0,
    /*banenr end*/
    currentTab: 0,
    _token:'',//key
    prodId : '',//商品id
    imgpath: '',//图片路径
    prodInfo:'',//产品详情
    specs:'',
    spec_values:[], // 选择的规格值
    prodInfo_content:'',//详情内容图片处理
    spec_background:'',
    imgs:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
    var that = this;
    var _prodId = options.id;
    var _key = wx.getStorageSync("token");
    var imgpath = app.globalData.imgsPath;
    if(options.agent_id) {
      wx.setStorageSync('agent_id', options.agent_id);
    }

    that.setData({
      _token: _key,
      prodId: _prodId,
      imgpath: imgpath
    })

    that.getproductInfo()
  },

  //点击切换
  clickTab: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current,
      })
    }
  },

  //获取产品详情
  getproductInfo: function () {
    var that = this;
    var _key = that.data._token;
    var _id = that.data.prodId;
    wx.request({
      url: app.globalData.httpsPath + '/product/info',
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      data: {
        token: _key,
        id: _id
      },
      //请求后台数据成功  
      success: function (res) {
        // console.info(res.data.data); return;
        var prod = res.data.data;
        if (prod.contents == null){
          that.setData({
            prodInfo_content: '',
          })
        }else{
          that.setData({
            prodInfo_content: prod.contents.replace(/<img /g, '<img class="rich_img" ')
          })
        }
        that.setData({
          prodInfo: prod,
          imgs :prod.imgs,
          specs:prod.specs,
        })
        // 隐藏加载框
        wx.hideLoading();
      }
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (options) {
    var that = this;
    var user_id = wx.getStorageInfoSync('user_id');
    if (options.from === 'button') {
      // 来自页面内转发按钮
      console.log(options.target)
    }
    return {
      title: options.target.dataset.name,
      path: '/pages/productDetails/productDetails?id=' + options.target.dataset.id + '&agent_id=' + user_id
    }
  },
  //加入购物车
  joinCartlist:function(){
    var that = this;
    var _token = wx.getStorageSync("token");
    if (_token == '') {
      wx.showToast({
        title: '请先登录',
        icon: 'fail',
        duration: 2000,
        mask: true
      })
      wx.navigateTo({
        url: '/pages/personalCenter/personalCenter',
      })

    }

    if (that.data.specs.length != that.data.spec_values.length) {
      console.info('请选择规格')
      console.info(that.data.specs.length, that.data.spec_values.lengt)
      wx.showToast({
        title: '请选择规格',
        icon: 'fail',
        duration: 2000,
        mask: true
      });
      return;
    }

    var _key = that.data._token;
    var _id = that.data.prodId;
    var _spec_values = that.data.spec_values;
    wx.request({
      url: app.globalData.httpsPath + '/cart/add',
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      data: {
        token: _key,
        product_id: _id,
        num:'1',
        spec_values: _spec_values,
      },
      //请求后台数据成功  
      success: function (res) {
        console.info(res);
        if(res.data.status == true){
          wx.showToast({
            title: '加入购物车成功',
            icon: 'succes',
            duration: 2000,
            mask: true
          })
        }
      }
    })
  },
  // 分享
  btnshare:function(){
    console.info(2)
  },
  //立即购物
  joinOrderlist:function(){
    var that = this;
    wx.removeStorageSync('cartItems');
    var _token = wx.getStorageSync("token");
    if (_token == '') {
      wx.showToast({
        title: '请先登录',
        icon: 'fail',
        duration: 2000,
        mask: true
      });
      wx.navigateTo({
        url: '/pages/personalCenter/personalCenter',
      })
    }

   
    if (that.data.specs.length != that.data.spec_values.length) {
      wx.showToast({
        title: '请选择规格',
        icon: 'fail',
        duration: 2000,
        mask: true
      });
      return ;
    }

    var that = this;
    var _key     = that.data._token;
    var _id      = that.data.prodId;
    var _spec_values = that.data.spec_values;
    wx.request({
      url: app.globalData.httpsPath + '/cart/add',
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      data: {
        token: _key,
        product_id: _id,
        spec_values: _spec_values,
        num: '1'
      },
      //请求后台数据成功  
      success: function (res) {
        console.info(res);
        if (res.data.status == true) {
          wx.navigateTo({
            url: '../shoppingCart/shoppingCart',
          })

        }
      }
    })
  },
  choseSpec:function(e){
    var that = this;
    var pics = [];
    var _specvalue = e.target.dataset.specvalue
    var _specid    = e.target.dataset.specid
    var _thumb     = e.target.dataset.thumb
    if(_thumb) {
      pics.push(_thumb)
      console.info(pics)
      that.setData({
       // imgs: pics
      })

     
      wx.previewImage({
        current: that.data.imgpath+_thumb, // 当前显示图片的http链接
        urls: [that.data.imgpath+_thumb]
      })
    }
    var arr = that.data.spec_values
    console.info(_specid)
    for (var key in arr) {
     
      console.log(arr[key])
      if (arr[key].indexOf(_specid+'_') != -1) {
        arr.splice(key, 1);       
      }
    }
   
   
   
    if (arr.indexOf(_specvalue) == -1) {
      arr.push(_specvalue)
    }
    that.setData({
      spec_values:arr
    })
   
    console.info(arr)
   
  },

 
})